package com.bsy.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bsy.dao.ConnectHelper;
import com.bsy.bean.Shuju;
import net.sf.json.JSONArray;

@WebServlet("/ShujuSelectServlet")
public class ShujuSelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConnectHelper ch = new ConnectHelper();
		Connection conn = ch.openConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from t_shuju2 where id=1");
			ResultSet rs = ps.executeQuery();
			ArrayList<Shuju> list = new ArrayList<Shuju>();

			while (rs.next()) {
				String qiyenumber = rs.getString("qiyenumber");
				String yonghunumber = rs.getString("yonghunumber");

				Shuju sj = new Shuju();
				sj.setQiyenumber(qiyenumber);
				sj.setYonghunumber(yonghunumber);

				list.add(sj);
			}
			rs.close();
			ps.close();
			conn.close();
			// 设置响应的数据编码格式
			response.setContentType("text/html;charset=utf-8");
			request.setAttribute("list", list);
			// 使用json将集合转为json字符串并响应到客户端
			JSONArray array = JSONArray.fromObject(list);
			System.out.println(array);
			response.getWriter().print(array.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
